<template>
  <el-dialog
    :before-close="close"
    :width="dialogWidth"
    :title="title"
    :visible.sync="isShow"
    v-if="isShow"
    v-dialogDrag
  >
    <el-form :model="form" ref="form" :rules="rules" class="overflow">
      <el-col :span="12" class="mb15">
        <el-form-item label="渠道：" prop="promotion" :label-width="formLabelWidth">
          <el-select placeholder="请选择渠道" v-model="form.promotion">
            <el-option v-for="item in promotionList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" class="mb15">
        <el-form-item label="数量：" prop="amount" :label-width="formLabelWidth">
          <el-input v-model="form.amount" autocomplete="off" placeholder="请输入数量(个)"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12" class="mb15">
        <el-form-item label="中心名：" prop="center" :label-width="formLabelWidth">
          <el-select placeholder="请选择中心名" v-model="form.center" @change="getDeptList">
            <el-option
              v-for="item in centerList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" class="mb15">
        <el-form-item label="部门名：" prop="dept" :label-width="formLabelWidth">
          <el-select placeholder="请选择部门名" v-model="form.dept">
            <el-option v-for="item in deptList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" class="mb15">
        <el-form-item label="时间：" prop="time" :label-width="formLabelWidth">
          <div class="block">
            <el-date-picker
              v-model="form.time"
              align="right"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions1"
            ></el-date-picker>
          </div>
        </el-form-item>
      </el-col>
      <el-col :span="12" class="mb15">
        <el-form-item label="费用：" prop="cost" :label-width="formLabelWidth">
          <el-input
            v-model="form.cost"
            autocomplete="off"
            placeholder="请输入费用(元)"
            @keyup.enter.native="handleConfirm('form')"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-form>
    <div slot="footer" class="dialog-footer align-center">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="handleConfirm('form')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import "../utils/drag.js";
import { pickerOptions1 } from "@/../config/Stats_options";

export default {
  name: "package_dialog",
  data() {
    var validateCost = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入费用！"));
      } else if (value.toString().search(/^[0-9]*$/) === -1) {
        return callback(new Error("请输入数字！"));
      } else if (value.length > 6) {
        return callback(new Error("长度在 1-6 个数字"));
      } else {
        callback();
      }
    };
    var validateAmount = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入数量！"));
      } else if (value.toString().search(/^[0-9]*$/) === -1) {
        return callback(new Error("请输入数字！"));
      } else if (value.length > 10) {
        return callback(new Error("长度在 1-10 个数字！"));
      } else {
        callback();
      }
    };
    return {
      form: {
        time: "",
        promotion: "",
        cost: "",
        amount: "",
        center: "",
        dept: ""
      },
      rules: {
        time: { required: true, message: "请选择分配时间！", trigger: "blur" },
        promotion: {
          required: true,
          message: "请选择推广渠道！",
          trigger: "blur"
        },
        cost: { validator: validateCost, trigger: "blur" },
        amount: { validator: validateAmount, trigger: "blur" },
        center: {
          required: true,
          message: "请选择分配中心！",
          trigger: "blur"
        },
        dept: { required: true, message: "请选择分配部门！", trigger: "blur" }
      },
      centerList: [],
      deptList: [],
      promotionList: [],
      pickerOptions1,
      formLabelWidth: "85px",
      dialogWidth: "700px"
    };
  },
  computed: {
    isShow() {
      return this.addDialog || this.editDialog;
    }
  },
  created() {
    this.getCenterList();
    this.getPromotionList();
  },
  watch: {
    editDialog() {
      this.form = {
        time: this.packageForm.time,
        promotion: this.packageForm.promotionName,
        cost: Number(this.packageForm.cost),
        amount: Number(this.packageForm.amount),
        center: this.packageForm.centerName,
        dept: this.packageForm.deptName
      };
    }
  },
  methods: {
    getDeptList() {
      this.form.dept = "";
      this.$axios.post("/api/system/departmentList", this.form).then(data => {
        this.deptList = data.map(item => {
          return item.name;
        });
      });
    },
    getPromotionList() {
      this.$axios.get("/api/system/promotions").then(data => {
        this.promotionList = data.map(item => {
          return item.name;
        });
      });
    },
    getCenterList() {
      this.$axios.get("/api/system/centers").then(data => {
        this.centerList = data;
      });
    },
    handleConfirm(formName) {
      // 先进行表单验证
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.editDialog ? this.editPackage() : this.addPackage();
        }
      });
    },
    addPackage() {
      this.$axios.post("/api/package/addPackage", this.form).then(data => {
        if (data.status) {
          this.$message({
            type: "success",
            message: "添加成功！"
          });
          this.$emit("update");
        } else {
          this.$message({
            type: "error",
            message: data.msg
          });
        }
      });
    },
    editPackage() {
      this.$axios
        .put("/api/package/editPackage/" + this.packageForm.id, this.form)
        .then(data => {
          if (data.status) {
            this.$message({
              type: "success",
              message: "编辑成功！"
            });
            this.$emit("update");
            this.close();
          } else {
            this.$message({
              type: "error",
              message: data.msg
            });
          }
        });
    },
    close() {
      this.$emit("close");
      this.resetForm();
    },
    resetForm() {
      this.form = {
        time: "",
        promotion: "",
        cost: "",
        amount: "",
        center: "",
        dept: ""
      };
    }
  },
  props: ["addDialog", "editDialog", "title", "packageForm"]
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
